﻿using System.Data;
using MySqlConnector;

namespace Front._1117._1124;

public partial class BookMain : Form
{
    public BookMain()
    {
        InitializeComponent();
    }
    

    private void BookMain_Load(object sender, EventArgs e)
    {
        // 获取所有图书
        GetAllBooks();
        
        
        //获取图书分类
        GetAllCategories();
        
        
    }
    
    private void GetAllCategories()
    {
        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql,connection);
        
        DataSet dataSet = new DataSet();
        
        mySqlDataAdapter.Fill(dataSet, "book_category");
        
        DataTable dataTable = dataSet.Tables["book_category"];
        
        DataRow row = dataTable.NewRow();
        row[0] = "全部";
        dataTable.Rows.InsertAt(row, 0);
        
        comboBox1.DataSource = dataTable;
        comboBox1.DisplayMember = dataTable.Columns[0].ColumnName;
    }
        
        
        
    private void GetAllBooks()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=Sun015492;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        string sql = "select * from book_info";
        //创建数据适配器
        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        //创建数据集
        DataSet dataSet = new DataSet();
        //填充数据
        mySqlDataAdapter.Fill(dataSet, "book_info");
        //绑定数据
        this.dataGridView1.DataSource = dataSet.Tables["book_info"];
    }
}